package com.lykj.modules.shop.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lykj.core.excel.util.ExcelUtil;
import com.lykj.core.tool.utils.DateUtil;
import com.lykj.modules.shop.excel.StoreProductExcel;
import com.lykj.modules.shop.znapi.ZnApiUtils;
import com.lykj.modules.system.entity.Region;
import com.lykj.modules.system.excel.RegionExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.lykj.core.mp.support.Condition;
import com.lykj.core.mp.support.Query;
import com.lykj.core.tool.api.R;
import com.lykj.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lykj.modules.shop.entity.StoreProduct;
import com.lykj.modules.shop.vo.StoreProductVO;
import com.lykj.modules.shop.wrapper.StoreProductWrapper;
import com.lykj.modules.shop.service.StoreProductService;
import com.lykj.core.boot.ctrl.LykjController;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

/**
 * 平台商品表 控制器
 *
 * @author cxl
 * @since 2022-01-13
 */
@RestController
@AllArgsConstructor
@RequestMapping("lykj-shop/store/product")
@Api(value = "商品信息", tags = "商品信息接口")
public class StoreProductController extends LykjController {

	private final StoreProductService storeProductService;
	private final ZnApiUtils znApiUtils;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入storeProduct")
	public R<StoreProduct> detail(StoreProduct storeProduct) {
		StoreProduct detail = storeProductService.getOne(Condition.getQueryWrapper(storeProduct));
		try {
			detail.setDetail(URLDecoder.decode(detail.getDetail(), "UTF-8"));
		} catch (Exception e) {
		}
		//同步挂单号和库存
//		if (Func.isBlank(detail.getZnMakerId())) {
//			JSONObject makerRecordSumInfo = znApiUtils.makerRecordSumInfo(detail.getZnCommodityId());
//			if ("200".equals(makerRecordSumInfo.getString("status")) && makerRecordSumInfo.getJSONObject("content") instanceof JSONObject) {
//				JSONArray sellMakerRecordList = makerRecordSumInfo.getJSONObject("content").getJSONArray("sellMakerRecordList");
//				if (sellMakerRecordList != null && sellMakerRecordList.size() > 0) {
//					JSONObject sellMakerRecord = sellMakerRecordList.getJSONObject(0);
//					detail.setZnMakerId(sellMakerRecord.getString("makerId"));
//					detail.setReserveCount(sellMakerRecord.getInteger("quantity"));
//					storeProductService.updateById(detail);
//				}
//			}
//		}
		return R.data(detail);
	}

	/**
	 * 详情
	 */
	@GetMapping("/info")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入storeProduct")
	public R<StoreProductVO> info(StoreProduct storeProduct) {
		StoreProduct detail = storeProductService.getOne(Condition.getQueryWrapper(storeProduct));
		try {
			detail.setDetail(URLDecoder.decode(detail.getDetail(), "UTF-8"));
		} catch (Exception e) {
		}
		if (detail.getIsIntegral() == 0 && !"0".equals(detail.getZnMakerId())) {
			//同步 挂单号
//			JSONObject makerRecordSumInfo = znApiUtils.makerRecordSumInfo(detail.getZnCommodityId());
//			if ("200".equals(makerRecordSumInfo.getString("status")) && makerRecordSumInfo.getJSONObject("content") instanceof JSONObject) {
//				JSONArray sellMakerRecordList = makerRecordSumInfo.getJSONObject("content").getJSONArray("sellMakerRecordList");
//				if (!Func.isNull(sellMakerRecordList) && sellMakerRecordList.size() > 0) {
//					JSONObject sellMakerRecord = sellMakerRecordList.getJSONObject(0);
//					detail.setZnMakerId(sellMakerRecord.getString("makerId"));
//					storeProductService.updateById(detail);
//				}
//			}
		}
		return R.data(StoreProductWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 平台商品表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入storeProduct")
	public R<IPage<StoreProductVO>> list(StoreProductVO storeProduct, Query query) {
		LambdaQueryWrapper<StoreProduct> queryWrapper = Wrappers.<StoreProduct>query().lambda();
		if (Func.isNotBlank(storeProduct.getTitle())) {
			queryWrapper.like(StoreProduct::getTitle, storeProduct.getTitle());
		}
		if (storeProduct.getCateId() != null) {
			queryWrapper.eq(StoreProduct::getCateId, storeProduct.getCateId());
		}
		if (Func.isNotBlank(storeProduct.getZnCommodityId())) {
			queryWrapper.eq(StoreProduct::getZnCommodityId, storeProduct.getZnCommodityId());
		}
		queryWrapper.eq(StoreProduct::getIsDeleted, 0);//未删除商品
		if (query.getAscs() == null && query.getDescs() == null) {
			query.setDescs("sort,create_time");
		}
//		IPage<StoreProduct> pages = storeProductService.page(Condition.getPage(query), queryWrapper);//Condition.getQueryWrapper(storeProduct)
		IPage<StoreProductVO> pages = storeProductService.selectStoreProductPage(Condition.getPage(query), storeProduct);
		return R.data(pages);
	}

	/**
	 * 自定义分页 平台商品表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入storeProduct")
	public R<IPage<StoreProductVO>> page(StoreProductVO storeProduct, Query query) {
		storeProduct.setStatus(1);//上架商品
		storeProduct.setIsDeleted(0);//未删除商品
		if (query.getAscs() == null && query.getDescs() == null) {
			query.setDescs("sort,create_time");
		}
		IPage<StoreProductVO> pages = storeProductService.selectStoreProductPage(Condition.getPage(query), storeProduct);
		return R.data(pages);
	}

	/**
	 * 新增 平台商品表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入storeProduct")
	public R save(@Valid @RequestBody StoreProduct storeProduct) {
//		try {
//			storeProduct.setDetail(URLDecoder.decode(storeProduct.getDetail(), "UTF-8"));
//		} catch (Exception e) {
//		}
		return R.status(storeProductService.save(storeProduct));
	}

	/**
	 * 修改 平台商品表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入storeProduct")
	public R update(@Valid @RequestBody StoreProduct storeProduct) {
		return R.status(storeProductService.updateById(storeProduct));
	}

	/**
	 * 新增或修改 平台商品表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入storeProduct")
	public R submit(@Valid @RequestBody StoreProduct storeProduct) {
		// 如果pv值为空或者小于等于0 则默认为商品售价
//		if (storeProduct.getIsIntegral() == 0 && (storeProduct.getPvPrice() == null || storeProduct.getPvPrice().compareTo(BigDecimal.ZERO) <= 0)) {
//			storeProduct.setPvPrice(storeProduct.getSellPrice());
//		}
		return R.status(storeProductService.saveOrUpdate(storeProduct));
	}

	/**
	 * 删除 平台商品表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(storeProductService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 导出商品数据
	 */
	@GetMapping("export")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "导出商品数据", notes = "传入product")
	public void exportRegion(@ApiIgnore @RequestParam Map<String, Object> param, HttpServletResponse response) {
		QueryWrapper<StoreProduct> queryWrapper = Condition.getQueryWrapper(param, StoreProduct.class);
		List<StoreProductExcel> list = storeProductService.export(queryWrapper);
		ExcelUtil.export(response, "商品数据" + DateUtil.time(), "商品数据", list, StoreProductExcel.class);
	}

}
